﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (RadioButtonSalaried.Checked)
        {
            TextBoxMonthlyIncome.Enabled = true;
            TextBoxEidChristmasBonus.Enabled = true;
            TextBoxPerformanceBonus.Enabled = true;
            TextBoxLeaveEncashment.Enabled = true;
            TextBoxOvertime.Enabled = true;
            TextBoxGratuity.Enabled = true;
            TextBoxOtherIncome.Enabled = true;

        }
        else if (RadioButtonSelf.Checked)
        {
            TextBoxMonthlyIncome.Enabled = false;
            TextBoxEidChristmasBonus.Enabled = false;
            TextBoxPerformanceBonus.Enabled = false;
            TextBoxLeaveEncashment.Enabled = false;
            TextBoxOvertime.Enabled = false;
            TextBoxGratuity.Enabled = false;
            TextBoxOtherIncome.Enabled = false;
        }
    }

    protected void ButtonCalculateIncomeTax_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            var totalTax = 0.0;

            var totalAnnualincome = double.Parse(TextBoxTotalTaxableIncome.Text.Length > 0 ? TextBoxTotalTaxableIncome.Text : "0");
            var monthlyIncome_UserInput = double.Parse(TextBoxMonthlyIncome.Text.Length > 0 ? TextBoxMonthlyIncome.Text : "0");

            if (totalAnnualincome <= 0 && monthlyIncome_UserInput > 0)
            {
                totalAnnualincome = monthlyIncome_UserInput * 12;
                TextBoxTotalTaxableIncome.Text = totalAnnualincome.ToString();
            }

            if (RadioButtonSalaried.Checked)
            {
                var eidChristmasBonus = double.Parse(TextBoxEidChristmasBonus.Text.Length > 0 ? TextBoxEidChristmasBonus.Text : "0");
                var performanceBonus = double.Parse(TextBoxPerformanceBonus.Text.Length > 0 ? TextBoxPerformanceBonus.Text : "0");
                var leaveEncashment = double.Parse(TextBoxLeaveEncashment.Text.Length > 0 ? TextBoxLeaveEncashment.Text : "0");
                var overtime = double.Parse(TextBoxOvertime.Text.Length > 0 ? TextBoxOvertime.Text : "0");
                var gratuity = double.Parse(TextBoxGratuity.Text.Length > 0 ? TextBoxGratuity.Text : "0");
                var otherIncome = double.Parse(TextBoxOtherIncome.Text.Length > 0 ? TextBoxOtherIncome.Text : "0");

                var allOtherIncomes_UserInput = eidChristmasBonus + performanceBonus + leaveEncashment + overtime + gratuity + otherIncome;

                bool isMonthlyIncomeValid = false;

                //Calculating monthly income from total annual income as input in TextBoxTotalTaxableIncome
                var calculatedMonthlyIncome = ((totalAnnualincome - allOtherIncomes_UserInput) / 12);

                /*
                 * If the difference between calculated monthly income and user entered monthly income is less than 100 
                 * then user entered monthly income is considered as valid and Net Monthly Income will be shown in the
                 * result panel
                 */
                if (monthlyIncome_UserInput > 0 && Math.Abs(calculatedMonthlyIncome - monthlyIncome_UserInput) < 100)
                    isMonthlyIncomeValid = true;

                //var totalAnnualincome = (monthyIncome * 12) + eidChristmasBonus + performanceBonus + leaveEncashment + overtime + otherIncome;
                //TextBoxTotalTaxableIncome.Text = Convert.ToString(totalAnnualincome);


                totalTax = NewITSalariedCalculator.CalculateTax(totalAnnualincome);
                var monthlyTax = totalTax / 12;

                LabelAnnualIncomeTax.Text = Math.Round(totalTax, 2).ToString("N");
                LabelMonthlyIncomeTax.Text = Math.Round(monthlyTax, 2).ToString("N");

                LabelNetAnnualSalary.Text = Math.Round((totalAnnualincome - totalTax), 2).ToString("N");

                var netMonthlySalary = Math.Round((monthlyIncome_UserInput - monthlyTax), 2);
                if (isMonthlyIncomeValid && netMonthlySalary >= 0)
                    LabelNetMonthlySalary.Text = netMonthlySalary.ToString("N");
                else
                    LabelNetMonthlySalary.Text = "-";
            }
            else if (RadioButtonSelf.Checked)
            {
                totalTax = NewITSelfCalculator.CalculateTax(totalAnnualincome);

                LabelAnnualIncomeTax.Text = Math.Round(totalTax, 2).ToString("N");
                LabelMonthlyIncomeTax.Text = Math.Round(totalTax / 12, 2).ToString("N");

                LabelNetAnnualSalary.Text = Math.Round((totalAnnualincome - totalTax), 2).ToString("N");
                LabelNetMonthlySalary.Text = "-";
            }

            if (totalAnnualincome <= 400000)
                LabelMessage.Text = "Total income is less than the minimum taxable income";

        }

    }
}